﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common.Model;
using BLL;
namespace stock.GuPiao
{
    public partial class Stock : System.Web.UI.Page
    {
        StockAlertBLL AlertBL = new StockAlertBLL();
        StockBLL stockBL = new StockBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                IniData();
            }
        }

        private void IniData()
        {
            List<StockAlert> alertList = new List<StockAlert>();
            if (Session["id"] != null)
            {
                alertList = AlertBL.GetModelList(" CustomerId="+Session["id"].ToString());
            }
            else
            {
                Response.Redirect("login.aspx");
                return;
            }
            GVproductclass.DataSource = alertList;
            GVproductclass.DataBind();

        }
        protected void GVproductclass_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lb_Name = (Label)e.Row.FindControl("lb_Name");
                lb_Name.Text = stockBL.GetModel(lb_Name.Text.Trim()).Name;
                lb_Name = (Label)e.Row.FindControl("lb_createtime");
                Label lb_UpAlertMargin = (Label)e.Row.FindControl("lb_UpAlertMargin");
                Label lb_DownAlertMargin = (Label)e.Row.FindControl("lb_DownAlertMargin");
                Label lb_XianJia = (Label)e.Row.FindControl("lb_XianJia");
                TextBox tb_UpAlertMargin = (TextBox)e.Row.FindControl("tb_UpAlertMargin");
                TextBox tb_DownAlertMargin = (TextBox)e.Row.FindControl("tb_DownAlertMargin");
                if (lb_Name != null)
                {
                    lb_Name.Text = Convert.ToDateTime(lb_Name.Text).ToString("yyyy-MM-dd HH:mm");
                }
                if (lb_UpAlertMargin != null)
                {
                    lb_UpAlertMargin.Text=(Decimal.Parse(lb_UpAlertMargin.Text)*100).ToString()+"%";
                }
                if (tb_UpAlertMargin != null)
                {
                    tb_UpAlertMargin.Text = (Decimal.Parse(tb_UpAlertMargin.Text) * 100).ToString();
                }
                if (lb_DownAlertMargin != null)
                {
                    lb_DownAlertMargin.Text = (Decimal.Parse(lb_DownAlertMargin.Text) * 100).ToString() + "%";
                }
                if (tb_DownAlertMargin != null)
                {
                    tb_DownAlertMargin.Text = (Decimal.Parse(tb_DownAlertMargin.Text) * 100).ToString();
                }
                if (lb_XianJia != null)
                {
                    String code = lb_XianJia.Text.Trim();
                    StockVirtual stockModel = new StockVirtual();
                    stockModel = Tools.GetStockByCode(code);
                    if (stockModel != null)
                    {
                        lb_XianJia.Text = stockModel.XianJia.ToString() + "￥";
                    }
                }
            }
        }
        protected void GVproductclass_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GVproductclass.DataKeys[e.RowIndex].Value.ToString();
            if (AlertBL.Delete(int.Parse(id)))
            {
                ClientScript.RegisterStartupScript(GetType(), "alert", "alert('删除成功！')", true);
                IniData();
                return;
            }
            ClientScript.RegisterStartupScript(GetType(), "alert", "alert('删除失败！')", true);
        }
        protected void GVCatagory_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GVproductclass.EditIndex = e.NewEditIndex;
            IniData();
        }
        protected void GVCatagory_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GVproductclass.EditIndex = -1;
            IniData();
        }
        protected void GVCatagory_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            
            int id = Convert.ToInt32(GVproductclass.DataKeys[e.RowIndex].Value);
            StockAlert AlertModel = AlertBL.GetModel(id);
            if (AlertModel != null)
            {
               TextBox tb_UpAlertPrice= ((TextBox)GVproductclass.Rows[e.RowIndex].FindControl("tb_UpAlertPrice"));
               TextBox tb_UpAlertMargin = ((TextBox)GVproductclass.Rows[e.RowIndex].FindControl("tb_UpAlertMargin"));
               TextBox tb_UpAuto = ((TextBox)GVproductclass.Rows[e.RowIndex].FindControl("tb_UpAuto"));
               TextBox tb_DownAlertPrice = ((TextBox)GVproductclass.Rows[e.RowIndex].FindControl("tb_DownAlertPrice"));
               TextBox tb_DownAlertMargin = ((TextBox)GVproductclass.Rows[e.RowIndex].FindControl("tb_DownAlertMargin"));
               TextBox tb_DownAuto = ((TextBox)GVproductclass.Rows[e.RowIndex].FindControl("tb_DownAuto"));
               if (tb_UpAlertPrice != null && tb_UpAlertMargin != null && tb_UpAuto != null && tb_DownAlertPrice != null && tb_DownAlertMargin != null && tb_DownAuto!=null)
                {
                    decimal UpPrice = Math.Abs(decimal.Parse(tb_UpAlertPrice.Text.Trim()));
                    decimal UpMargin = Math.Abs(decimal.Parse(tb_UpAlertMargin.Text.Trim()));
                    decimal UpAuto = Math.Abs(decimal.Parse(tb_UpAuto.Text.Trim()));
                    decimal DownPrice = Math.Abs(decimal.Parse(tb_DownAlertPrice.Text.Trim()));
                    decimal DownMargin = 0 - Math.Abs(decimal.Parse(tb_DownAlertMargin.Text.Trim()));
                    decimal DownAuto = Math.Abs(decimal.Parse(tb_DownAuto.Text.Trim()));
                    String script;
                    if (DownMargin > 100)
                    {
                        script = "<script language='Javascript'>alert('跌幅不可能达到100%！');</script>";
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", script);
                        return;
                    }
                    if (DownPrice >= UpPrice)
                    {
                        script = "<script language='Javascript'>alert('下跌提醒价格不能超过上扬价格');</script>";
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", script);
                        return;
                    }
                    AlertModel.DownAlertMargin = DownMargin / 100;
                    AlertModel.DownAlertPrice = DownPrice;
                    AlertModel.DownAuto = DownAuto;
                    AlertModel.UpAlertMargin = UpMargin / 100;
                    AlertModel.UpAlertPrice = UpPrice;
                    AlertModel.UpAuto = UpAuto;
                    if (AlertBL.Update(AlertModel))
                    {
                        script = "<script language='Javascript'>alert('更新成功！');</script>";
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", script);
                        GVproductclass.EditIndex = -1;
                        IniData();
                        return;
                    }
                    else
                    {
                        script = "<script language='Javascript'>alert('遇到未知错误，更新失败');</script>";
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", script);
                        GVproductclass.EditIndex = -1;
                        IniData();
                        return;
                    }                   
                }
            }
        }
        protected void btAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddGuPiao.aspx");
        }
    }
}